n = 10005
fa = [i for i in range(n)]
vis = set()
from sys import setrecursionlimit

setrecursionlimit(n)


def find(x):
    if x != fa[x]: fa[x] = find(fa[x])
    return fa[x]


def merge(x, y):
    x, y = find(x), find(y)
    if x != y: fa[y] = x


for _ in range(int(input())):
    a = list(map(int, input().split()))[1:]
    for i in a:
        vis.add(i)
        merge(a[0], i)

num = 0
for i in vis:
    num += i == find(i)

print(num, len(vis))

for _ in range(int(input())):
    u, v = map(int, input().split())
    print('Yes' if find(u) == find(v) else 'No')
